package com.studyinghome.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.toolkit.MPJWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.studyinghome.dto.UserDTO;
import com.studyinghome.entity.Address;
import com.studyinghome.entity.Area;
import com.studyinghome.entity.User;
import com.studyinghome.mapper.UserMapper;
import com.studyinghome.service.UserService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Leslie
 * @create 2023-01-04 16:48
 **/
@Service
public class UserServiceImpl extends MPJBaseServiceImpl<UserMapper, User> implements UserService {

    @Override
    public List<UserDTO> getList() {
        MPJLambdaWrapper<User> wrapper = MPJWrappers.lambdaJoin();
        wrapper.selectAll(User.class)
                .selectAs(User::getHeadImg,UserDTO::getUserHeadImg)
                .selectCollection(Address.class, UserDTO::getAddressList)
                .leftJoin(Address.class, Address::getUserId, User::getId)
                .orderByDesc(User::getId);

        return baseMapper.selectJoinList(UserDTO.class, wrapper);
    }

    @Override
    public IPage<UserDTO> getPage(int current, int size) {
        IPage<UserDTO> page = new Page<>(current, size);

        MPJLambdaWrapper<User> wrapper = MPJWrappers.lambdaJoin();
        wrapper.selectAll(User.class)
                .selectAll(Address.class)
                .selectAll(Area.class)
                .leftJoin(Address.class, Address::getUserId, User::getId)
                .leftJoin(Area.class, Area::getId, Address::getAreaId);

        return baseMapper.selectJoinPage(page, UserDTO.class, wrapper);
    }
}
